<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/murmur/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/murmur/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/murmur/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/murmur/images/logo.svg" color="#222">

<link rel="stylesheet" href="/murmur/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/murmur/","scheme":"Gemini","version":"8.0.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="第5章 ：原型5.1 [[Prototype]] JavaScript的对象都有一个[[Prototype]]内置属性，它是一个对象的引用。对象在创建时[[Prototype]]属性就会被赋予值。12345&#x2F;&#x2F;举例：创建一个myObj对象，然后读取其属性值var myObj &#x3D; &amp;#123;    a : 2&amp;#125;;myObj.a;    &#x2F;&#x2F; 2 之前说过，当访问对象的属性时，会触发[[G">
<meta property="og:type" content="article">
<meta property="og:title" content="《你不知道的JavaScript(上)》S02E05">
<meta property="og:url" content="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E05/index.html">
<meta property="og:site_name" content="汇话板">
<meta property="og:description" content="第5章 ：原型5.1 [[Prototype]] JavaScript的对象都有一个[[Prototype]]内置属性，它是一个对象的引用。对象在创建时[[Prototype]]属性就会被赋予值。12345&#x2F;&#x2F;举例：创建一个myObj对象，然后读取其属性值var myObj &#x3D; &amp;#123;    a : 2&amp;#125;;myObj.a;    &#x2F;&#x2F; 2 之前说过，当访问对象的属性时，会触发[[G">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-12-01T14:28:40.000Z">
<meta property="article:modified_time" content="2020-12-01T15:07:25.687Z">
<meta property="article:author" content="William Leung">
<meta property="article:tag" content="你不知道的JavaScript">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E05/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>《你不知道的JavaScript(上)》S02E05 | 汇话板</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?a49081c570a782a242cdf6e19e1a584d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/murmur/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">汇话板</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">设定目标，放慢步调，用温和的力量自我改革</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/murmur/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/murmur/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AC%AC5%E7%AB%A0-%EF%BC%9A%E5%8E%9F%E5%9E%8B"><span class="nav-number">1.</span> <span class="nav-text">第5章 ：原型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-Prototype"><span class="nav-number">1.0.1.</span> <span class="nav-text">5.1 [[Prototype]]</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#5-1-1-object-prototype"><span class="nav-number">1.0.1.0.1.</span> <span class="nav-text">5.1.1 object.prototype</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-1-2-%E5%B1%9E%E6%80%A7%E8%AE%BE%E7%BD%AE%E5%92%8C%E5%B1%8F%E8%94%BD"><span class="nav-number">1.0.1.0.2.</span> <span class="nav-text">5.1.2 属性设置和屏蔽</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E2%80%9C%E7%B1%BB%E2%80%9D"><span class="nav-number">1.0.2.</span> <span class="nav-text">5.2 “类”</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-1-%E2%80%9C%E7%B1%BB%E2%80%9D%E5%87%BD%E6%95%B0"><span class="nav-number">1.0.2.0.1.</span> <span class="nav-text">5.2.1 “类”函数</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#%E5%85%B3%E4%BA%8E%E5%90%8D%E7%A7%B0"><span class="nav-number">1.0.2.0.1.1.</span> <span class="nav-text">关于名称</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-2-%E2%80%9C%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E2%80%9D"><span class="nav-number">1.0.2.0.2.</span> <span class="nav-text">5.2.2 “构造函数”</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-3-%E6%8A%80%E6%9C%AF"><span class="nav-number">1.0.2.0.3.</span> <span class="nav-text">5.2.3 技术</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#%E5%9B%9E%E9%A1%BE%E2%80%9C%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E2%80%9D"><span class="nav-number">1.0.2.0.3.1.</span> <span class="nav-text">回顾“构造函数”</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-%E5%8E%9F%E5%9E%8B%E7%BB%A7%E6%89%BF"><span class="nav-number">1.0.3.</span> <span class="nav-text">5.3 原型继承</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#%E6%A3%80%E6%9F%A5%E2%80%9C%E7%B1%BB%E2%80%9D%E5%85%B3%E7%B3%BB"><span class="nav-number">1.0.3.0.0.1.</span> <span class="nav-text">检查“类”关系</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-%E5%AF%B9%E8%B1%A1%E5%85%B3%E8%81%94"><span class="nav-number">1.0.4.</span> <span class="nav-text">5.4 对象关联</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#5-4-1-%E5%88%9B%E5%BB%BA%E5%85%B3%E8%81%94"><span class="nav-number">1.0.4.0.1.</span> <span class="nav-text">5.4.1 创建关联</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-4-2-%E5%85%B3%E8%81%94%E5%85%B3%E7%B3%BB%E6%98%AF%E5%A4%87%E7%94%A8"><span class="nav-number">1.0.4.0.2.</span> <span class="nav-text">5.4.2 关联关系是备用</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-5-%E5%B0%8F%E7%BB%93"><span class="nav-number">1.0.5.</span> <span class="nav-text">5.5 小结</span></a></li></ol></li></ol></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">William Leung</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/murmur/archives">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/murmur/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/murmur/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E05/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/murmur/images/avatar.gif">
      <meta itemprop="name" content="William Leung">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="汇话板">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          《你不知道的JavaScript(上)》S02E05
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2020-12-01 22:28:40 / 修改时间：23:07:25" itemprop="dateCreated datePublished" datetime="2020-12-01T22:28:40+08:00">2020-12-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/murmur/categories/%E4%B8%80%E5%91%A8%E4%B8%80%E7%AB%A0%E5%89%8D%E7%AB%AF%E4%B9%A6/" itemprop="url" rel="index"><span itemprop="name">一周一章前端书</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>6 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="第5章-：原型"><a href="#第5章-：原型" class="headerlink" title="第5章 ：原型"></a>第5章 ：原型</h1><h3 id="5-1-Prototype"><a href="#5-1-Prototype" class="headerlink" title="5.1 [[Prototype]]"></a>5.1 [[Prototype]]</h3><ul>
<li>JavaScript的对象都有一个<code>[[Prototype]]</code>内置属性，它是一个对象的引用。对象在创建时<code>[[Prototype]]</code>属性就会被赋予值。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//举例：创建一个myObj对象，然后读取其属性值</span></span><br><span class="line"><span class="keyword">var</span> myObj = &#123;</span><br><span class="line">    a : <span class="number">2</span></span><br><span class="line">&#125;;</span><br><span class="line">myObj.a;    <span class="comment">// 2</span></span><br></pre></td></tr></table></figure></li>
<li>之前说过，当访问对象的属性时，会触发<code>[[Get]]</code>操作。<code>[[Get]]</code>操作第一步是检查对象本身是否有这个属性，如果有就使用；但是如果没有这个属性，就需要使用对象的<code>[[Prototype]]</code>链了。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> anotherObj = &#123;</span><br><span class="line">    a : <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// Object.create()方法是根据传入的对象，创建一个新对象，并且原型链上关联到传入的对象</span></span><br><span class="line"><span class="keyword">var</span> myObj = <span class="built_in">Object</span>.create(anotherObj);</span><br><span class="line">myObj.a;  <span class="comment">// 2</span></span><br></pre></td></tr></table></figure></li>
<li>实际上，所有访问属性的操作，都会查询<code>[[Prototype]]</code>链，包括<code>for..in</code>遍历、<code>in</code>操作等。<h5 id="5-1-1-object-prototype"><a href="#5-1-1-object-prototype" class="headerlink" title="5.1.1 object.prototype"></a>5.1.1 object.prototype</h5></li>
<li>所有的<code>[[Prototype]]</code>的尽头都执行内置的<code>object.prototype</code>对象，导致所有的普通对象都包含<code>Object.prototype</code>对象的许多通用的功能，比如<code>toString()</code>、<code>valueOf()</code>、<code>hasOwnProperty()</code>、<code>isPrototypeOf()</code>等方法。<h5 id="5-1-2-属性设置和屏蔽"><a href="#5-1-2-属性设置和屏蔽" class="headerlink" title="5.1.2 属性设置和屏蔽"></a>5.1.2 属性设置和屏蔽</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">myObj.foo = <span class="string">&#x27;bar&#x27;</span>;</span><br></pre></td></tr></table></figure></li>
<li>当通过.操作符访问对象进行属性赋值/属性追加时，有如下几种情况：<ul>
<li>如果当前对象中包含该属性，则修改当前对象的属性值（此时即便对象的原型链上包含同名的属性，根据就近原则，也会被当前对象的属性值屏蔽）</li>
<li>如果当前对象不包含该属性，就会遍历查找对象的原型链是否有该属性：<ul>
<li>如果在<code>[[Prototype]]</code>链上也不包含该属性，属性会被直接添加到当前对象<code>myObj</code>上；</li>
<li>如果在<code>[[Prototype]]</code>链上存在该属性：<ul>
<li>且属性非只读(<code>writable:false</code>)，那么在当前对象上添加一个名为<code>foo</code>的新属性，系统视它为继承了原型对象的屏蔽属性；</li>
<li>但属性是只读(<code>writable:true</code>)，那么将无法修改属性，也无法创建新属性。允许在严格模式下，代码会抛出一个错误；非严格模式下，这条赋值语句会被忽略；</li>
<li>且属性是个<code>setter</code>，那就一定会调用这个<code>setter</code>，属性的赋值屏蔽于<code>myObj</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>值得注意的是，<code>++</code>操作符会造成隐式屏蔽<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> anotherObj = &#123;</span><br><span class="line">    a : <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//创建一个原型关联anotherObj的新对象</span></span><br><span class="line"><span class="keyword">var</span> myObj = <span class="built_in">Object</span>.create(anotherObject);</span><br><span class="line"><span class="comment">//不管子对象还是父对象，a属性值都输出2</span></span><br><span class="line">anotherObj.a;   <span class="comment">// 2</span></span><br><span class="line">myObjt.a;   <span class="comment">// 2</span></span><br><span class="line"><span class="comment">//父对象中拥有a，而子对象中不拥有a</span></span><br><span class="line">anotberObj.hasOwnProperty(<span class="string">&#x27;a&#x27;</span>); <span class="comment">// true</span></span><br><span class="line">myObj.hasOwnProperty(<span class="string">&#x27;a&#x27;</span>);  <span class="comment">//false</span></span><br><span class="line"><span class="comment">//执行++操作，造成隐式屏蔽</span></span><br><span class="line">myObj.a++;</span><br><span class="line"><span class="comment">//再看发现子类自己拥有了a属性，屏蔽了原型链上的同名属性</span></span><br><span class="line">anotherObj.a;   <span class="comment">// 2</span></span><br><span class="line">myObj.a;    <span class="comment">// 3</span></span><br><span class="line">myObj.hasOwnProperty(<span class="string">&#x27;a&#x27;</span>);  <span class="comment">// ture</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="5-2-“类”"><a href="#5-2-“类”" class="headerlink" title="5.2 “类”"></a>5.2 “类”</h3><ul>
<li>如第4章提到的，JavaScript中没有类。面向类的语言需要通过类作为对象的蓝图来创建对象，而JavaScript是直接创建对象的。实际上JavaScript才是真正应该被称为“面向对象”的语言。<h5 id="5-2-1-“类”函数"><a href="#5-2-1-“类”函数" class="headerlink" title="5.2.1 “类”函数"></a>5.2.1 “类”函数</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Foo();</span><br><span class="line"><span class="built_in">Object</span>.getPrototypeOf(a) === Foo.prototype; <span class="comment">// ture</span></span><br></pre></td></tr></table></figure></li>
<li>如上面代码所示，<code>new Foo()</code>会生成一个新对象<code>a</code>，新对象内部的<code>[[Prototype]]</code>链关联到了<code>Foo.prototype</code>原型对象。</li>
<li>这里可以对比其他面向类的语言中，类可以被实例化多次，就像用模具制作东西一样，而实例化就意味着“把类的行为复制到物理对象中”，每一个新实例都会重复这个过程。但JavaScript没有类似的复制机制，只是通过<code>[[Prototype]]</code>原型链将对象关联起来。<h6 id="关于名称"><a href="#关于名称" class="headerlink" title="关于名称"></a>关于名称</h6></li>
<li>这个通过<code>[[Prototype]]</code>将对象关联起来的机制被称为“ <strong>原型继承</strong> ”</li>
<li>但原型继承这个术语比较容易造成混淆，影响大家对JavaScript机制真实原理的理解。毕竟继承意味着复制操作，但JavaScript并不会复制对象属性。而是在对象之间穿件一个关联，对象通过委托访问另一个对象的属性和函数。 <strong>委托</strong> 这个术语更能准确的描述JavaScript中对象的关联机制。</li>
<li><strong>差异继承</strong> 就是指在描述对象行为时，不描述普遍的特质。比如，描述汽车时，我们更多会说“汽车是一个有四个轮子的交通工具”，但不会说“汽车是拥有引擎发动机(通用特质)的交通工具”</li>
</ul>
<h5 id="5-2-2-“构造函数”"><a href="#5-2-2-“构造函数”" class="headerlink" title="5.2.2 “构造函数”"></a>5.2.2 “构造函数”</h5><ul>
<li>之所以让我们认为<code>Foo</code>是一个“类”，一个原因是关键字<code>new</code>，在面向类的语言中构造类实例时也会用到<code>new</code>；另一个原因是，<code>Foo()</code>的调用看起来像是执行了类的构造函数方法。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line">Foo.prototype.constructor === Foo;  <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Foo();</span><br><span class="line">a.constructor === Foo;  <span class="comment">// true</span></span><br></pre></td></tr></table></figure></li>
<li>可见<code>Foo.prototype.constructor</code>属性和<code>Foo</code>函数相等，创建的<code>a</code>对象的<code>constructor</code>属性，和<code>Foo</code>函数也相等。</li>
<li>凡此种种，我们很容易就误认为<code>Foo()</code>是一个构造函数。但实际上，<code>Foo()</code>本身并不是构造函数，和其他普通的函数没有任何区别。只是，在函数调用的前面加上<code>new</code>关键字后，变成一种“构造函数的调用方式”。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">NothingSpecial</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;Don&#x27;t mind me&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> NothingSpecial(); <span class="comment">// Don&#x27;t mind me</span></span><br><span class="line">a; <span class="comment">// &#123;&#125;</span></span><br></pre></td></tr></table></figure></li>
<li>如上述代码所示，<code>NothingSpecial</code>只是一个普通的函数，使用<code>new</code>调用时，它会构造一个对象并赋值给变量<code>a</code>。</li>
<li>对于“构造函数”更准确的解释，应该是“带有new的函数调用”。</li>
</ul>
<h5 id="5-2-3-技术"><a href="#5-2-3-技术" class="headerlink" title="5.2.3 技术"></a>5.2.3 技术</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义一个Foo函数，函数里将this.name属性重新赋值</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//为Foo.prototype原型对象定义一个myName()方法，返回name值</span></span><br><span class="line">Foo.prototype.myName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.name;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//通过new的方式调用Foo()函数，返回a、b两个对象</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Foo(<span class="string">&#x27;a&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> Foo(<span class="string">&#x27;b&#x27;</span>);</span><br><span class="line"><span class="comment">//调用a、b对象的myName()方法</span></span><br><span class="line">a.myName(); <span class="comment">// &#x27;a&#x27;</span></span><br><span class="line">b.myName(); <span class="comment">// &#x27;b&#x27;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>看这段代码，调用了<code>new Foo()</code>后，创建的<code>a</code>和<code>b</code>对象都拥有了<code>myName()</code>方法。</li>
<li>看起来像是在创建<code>a</code>和<code>b</code>时，把<code>Foo.prototype</code>原型对象复制到两个对象中，但事实上并不是。</li>
<li>正如本章前文介绍<code>[[Get]]</code>算法时提到的，当访问对象的属性不存在时，会通过其的<code>[[[Prototype]]</code>原型链来查找。因此，在创建的过程中，<code>a</code>和<code>b</code>的<code>[[Prototype]]</code>原型对象会关联到<code>Foo.prototype</code>上。当访问<code>a.myName</code>时会通过原型链，委托关联到<code>Foo.prototype</code></li>
</ul>
<h6 id="回顾“构造函数”"><a href="#回顾“构造函数”" class="headerlink" title="回顾“构造函数”"></a>回顾“构造函数”</h6><ul>
<li><p>之前讨论<code>.constructor</code>属性时说过，虽然<code>a.constructor === Foo</code>为<code>true</code>，看起来<code>a</code>的构造函数(<code>constructor</code>)就是<code>Foo()</code>函数，但事实不是这样的。就如同<code>myName</code>属性一样，<code>a</code>对象本身并无<code>.constructor</code>属性，查找的其实是<code>Foo.prototype</code>原型对象的<code>myName</code>属性（也可以说，<code>a.constructor</code>被委托给了<code>Foo.prototype</code>）。而<code>Foo.prototype</code>本身的<code>.constructor</code>属性，是在<code>Foo</code>函数声明时的默认属性。</p>
</li>
<li><p>我们可以做个试验来验证这一点：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义Foo函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params"></span>)</span>&#123;&#125;;</span><br><span class="line"><span class="comment">//将Foo的原型对象赋值为空</span></span><br><span class="line">Foo.prototype = &#123;&#125;;</span><br><span class="line"><span class="comment">//创建对象a</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Foo();</span><br><span class="line"><span class="comment">//测试</span></span><br><span class="line"><span class="built_in">console</span>.log(a.constructor === Foo);    <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(a.constructor === <span class="built_in">Object</span>);  <span class="comment">// true </span></span><br></pre></td></tr></table></figure></li>
<li><p><code>a.constructor</code>属性本来委托给<code>Foo.prototype</code>，但我们已经把<code>Foo.prototype</code>重新赋值为空，所以她继续委托，直至最顶端的<code>Object.prototype</code>。</p>
</li>
<li><p>总之，对象的<code>.constructor</code>属性并不表示构造对象的函数。它只是一个不可变、且不可枚举，但可以被修改的属性，所以<code>.constructor</code>属性是不可靠且不安全的引用。</p>
<h3 id="5-3-原型继承"><a href="#5-3-原型继承" class="headerlink" title="5.3 原型继承"></a>5.3 原型继承</h3></li>
<li><p><code>Object.create()</code>方法会创建一个新对象，并把对象内部的<code>[[Prototype]]</code>关联到指定的对象，但缺点是通过创建一个新对象替代旧对象，而不是直接修改已有对象。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * ----------------------</span></span><br><span class="line"><span class="comment"> * 【示例：继承JS类并进行实例化】</span></span><br><span class="line"><span class="comment"> *  西瓜继承自水果，水果拥有type(食物类型)属性，西瓜拥有name(水果名称)属性</span></span><br><span class="line"><span class="comment"> * ----------------------</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//声明Fruit对象，并定义getType()方法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Fruit</span>(<span class="params">type</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.type = type;</span><br><span class="line">&#125;</span><br><span class="line">Fruit.prototype.getType = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.type;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//声明WateMelon对象，继承Fruit，并拥有getName()方法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">WateMelon</span>(<span class="params">type,name</span>)</span>&#123;</span><br><span class="line">    Fruit.call(<span class="built_in">this</span>,type);</span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//通过Object.create()来继承</span></span><br><span class="line">WateMelon.prototype = <span class="built_in">Object</span>.create(Fruit.prototype);</span><br><span class="line">WateMelon.prototype.getName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建实例</span></span><br><span class="line"><span class="keyword">var</span> watemelon = <span class="keyword">new</span> WateMelon(<span class="string">&#x27;水果&#x27;</span>,<span class="string">&#x27;西瓜&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;食物品种：&#x27;</span>,watemelon.getType());</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;食物名称：&#x27;</span>,watemelon.getName());</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>想要直接修改对象的<code>[[Prototype]]</code>关联，在ES6之前，只能通过<code>.__proto__</code>属性来设置，但这个属性不能兼容所有浏览器，ES6添加了<code>Object.setPrototypeOf()</code>方法来实现。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.setPrototypeOf(WateMelon.prototype,Fruit.prototype);</span><br></pre></td></tr></table></figure>
<h6 id="检查“类”关系"><a href="#检查“类”关系" class="headerlink" title="检查“类”关系"></a>检查“类”关系</h6></li>
<li><p>我们如何知道<code>watemelon</code>的委托对象是<code>WateMelon</code>呢？在传统的面向类环境中，检查实例对象的继承祖先通常被称为内省或者反射。</p>
</li>
<li><p>第一种方法是站在“类函数”的角度来判断，通过<code>instanceof</code>操作符判断实例对象是否属于某个类函数：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">watemelon <span class="keyword">instanceof</span> Watemelon; <span class="comment">// true</span></span><br><span class="line">watemelon <span class="keyword">instanceof</span> Fruit; <span class="comment">// true</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>instanceof</code>操作符的左边是一个实例对象<code>watemelon</code>，右边是一个函数。<code>instanceof</code>会检查在<code>watemelon</code>的原型链中，是否有指向<code>Watemelon</code>或者<code>Fruit</code>的原型对象。</p>
<blockquote>
<p>注意：<code>instanceof</code> 判断的是实例对象和类的原型关联，而如果想判断两个实例对象之间是否有原型关联，则<code>instanceof</code>无法实现。</p>
</blockquote>
</li>
<li><p>第二种方法是通过<code>.isPrototypeOf()</code>方法，判断原型对象是否和指定的实例对象有关系：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">WateMelon.prototype.isPrototypeOf(watemelon);</span><br></pre></td></tr></table></figure></li>
<li><p><code>isPrototypeOf()</code>是基类<code>Object</code>的方法，判断watemelon的原型对象是不是Watemelon。</p>
<blockquote>
<p>注意：一定是要用原型对象调用<code>isPrototypeOf()</code>方法来做判断，如果用类函数(<code>WateMelon</code>)来调用，则返回<code>false</code>。</p>
</blockquote>
</li>
<li><p>除此之外，还可以通过<code>Object.getPrototypeOf(watemelon)</code>来获取实例对象的原型链。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.getPrototypeOf(watemelon) === WateMelon.prototype;   <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="5-4-对象关联"><a href="#5-4-对象关联" class="headerlink" title="5.4 对象关联"></a>5.4 对象关联</h3><h5 id="5-4-1-创建关联"><a href="#5-4-1-创建关联" class="headerlink" title="5.4.1 创建关联"></a>5.4.1 创建关联</h5><ul>
<li>我们推荐用<code>create()</code>方法创建新对象，它可以充分发挥<code>[[Prototype]]</code>机制的威力，而是用new构造函数调用的方式，通过类来创建两个对象之间的关系，避免了一些不必要的麻烦，比如<code>.prototype</code>和<code>.constructor</code>属性引用的问题。</li>
<li>由于<code>Object.create()</code>方法是ES5新增的函数，如果想要在ES5之前的环境使用，可以通过简单的polyfill代码来处理：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(!<span class="built_in">Object</span>.create)&#123;</span><br><span class="line">    <span class="built_in">Object</span>.crate = <span class="function"><span class="keyword">function</span>(<span class="params">o</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//通过一个临时的F类，建立指定原型关系</span></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">F</span>(<span class="params"></span>)</span>&#123;&#125;;</span><br><span class="line">        F.prototype = o;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> F();</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="5-4-2-关联关系是备用"><a href="#5-4-2-关联关系是备用" class="headerlink" title="5.4.2 关联关系是备用"></a>5.4.2 关联关系是备用</h5></li>
<li>通过<code>[[Prototype]]</code>原型机制可以为对象之间建立关联关系，处理当对象属性或方法“缺失”时，可以提供一个备用选项。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = &#123;</span><br><span class="line">    echo : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;hello&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = <span class="built_in">Object</span>.create(a);</span><br><span class="line">b.echo();   <span class="comment">// &#x27;hello&#x27;</span></span><br></pre></td></tr></table></figure></li>
<li>但从某种程度上来看，代码却变得难以理解和维护，<code>b</code>明明没有定义<code>echo()</code>方法，这东西是哪来的？</li>
<li>当然，我们可以通过 <strong>内部委托</strong> 的方式让API的设计更加清晰：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = &#123;</span><br><span class="line">    echo : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;hello&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = <span class="built_in">Object</span>.create(a);</span><br><span class="line">b.doEcho = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.echo();</span><br><span class="line">&#125;;</span><br><span class="line">b.doEcho();   <span class="comment">// &#x27;hello&#x27;</span></span><br></pre></td></tr></table></figure>


</li>
</ul>
<h3 id="5-5-小结"><a href="#5-5-小结" class="headerlink" title="5.5 小结"></a>5.5 小结</h3><ul>
<li>当访问对象中并不存在的属性时，<code>[[Get]]</code>操作会查找对象内部<code>[[Prototype]]</code>关联的对象。这个关联关系，实际上定义了一条“原型链”。</li>
<li>原型链的顶端是<code>Object.prototype</code>，<code>toString()</code>、<code>valueOf()</code>和其他通用的功能都挂载在该对象上。</li>
<li>关联两个对象最常用的方式是用<code>new</code>关键字进行函数调用，通常我们称为“构造函数调用”。</li>
<li>虽然JavaScript的<code>new</code>机制看起来，和传统面向类语言的“类初始化”和“类继承”很相似，但是有一个很重要的区别，就是不会进行复制，对象之间是通过内部的<code>[[Prototype]]</code>链关联的。</li>
<li>出于各种原因，相比起术语“原型继承”，“委托”更适合。因为对象之间的关系不是复制，而是委托。</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>William Leung
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E05/" title="《你不知道的JavaScript(上)》S02E05">https://gitee.com/lianghuilin/murmur.git/reading_notes/《你不知道的JavaScript-上-》S02E05/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/murmur/tags/%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript/" rel="tag"># 你不知道的JavaScript</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/murmur/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E04/" rel="prev" title="《你不知道的JavaScript(上)》S02E04">
                  <i class="fa fa-chevron-left"></i> 《你不知道的JavaScript(上)》S02E04
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/murmur/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E06/" rel="next" title="《你不知道的JavaScript(上)》S02E06">
                  《你不知道的JavaScript(上)》S02E06 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">William Leung</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">142k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:09</span>
  </span>
</div>
<div class="busuanzi-count">
    <span id="busuanzi_container_site_uv">
      本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/murmur/js/utils.js"></script><script src="/murmur/js/motion.js"></script><script src="/murmur/js/next-boot.js"></script>

  
















  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  

</body>
</html>
